package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import java.util.logging.Level;
import java.util.logging.Logger;

public class VALUE extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    String str = paramArrayOfObject[0].toString();
    Object localObject1 = paramArrayOfObject[1];
    int i = -1;
    FArray localFArray1 = new FArray();
    Calculator localCalculator = getCalculator();
    TableData localTableData = DataUtils.getTableData4Function(localCalculator, str);
    if (localObject1 instanceof String)
      i = columnToInt(localTableData, localObject1.toString());
    else if (localObject1 instanceof Integer)
      i = Integer.parseInt(localObject1.toString());
    try
    {
      if ((localTableData == null) || (i > localTableData.getColumnCount()) || (i < 0))
        return Primitive.NULL;
      for (int j = 0; j < localTableData.getRowCount(); ++j)
        localFArray1.add(localTableData.getValueAt(j, i - 1));
      if (paramArrayOfObject.length > 2)
      {
        if (paramArrayOfObject.length > 3)
        {
          Object localObject2 = paramArrayOfObject[2];
          int l = -1;
          if (localObject2 instanceof String)
            l = columnToInt(localTableData, localObject2.toString());
          else if (localObject2 instanceof Integer)
            l = Integer.parseInt(localObject2.toString());
          if ((l > localTableData.getColumnCount()) || (l < 1))
            return Primitive.NULL;
          Object localObject3 = paramArrayOfObject[3];
          FArray localFArray2 = new FArray();
          for (int i1 = 0; i1 < localFArray1.length(); ++i1)
            if (localObject3.equals(localTableData.getValueAt(i1, l - 1)))
              localFArray2.add(localFArray1.elementAt(i1));
          if (localFArray2.length() == 0)
            return Primitive.NULL;
          if (paramArrayOfObject.length == 4)
            return localFArray2;
          i1 = 0;
          try
          {
            i1 = Integer.parseInt(paramArrayOfObject[4].toString()) - 1;
          }
          catch (Exception localException)
          {
          }
          if (i1 < 0)
            i1 = 0;
          else if (i1 > localFArray2.length() - 1)
            i1 = localFArray2.length() - 1;
          return localFArray2.elementAt(i1);
        }
        int k = Integer.parseInt(paramArrayOfObject[2].toString());
        if ((k > localTableData.getRowCount()) || (k < 1))
          return Primitive.NULL;
        return localFArray1.elementAt(k - 1);
      }
      return localFArray1;
    }
    catch (TableDataException localTableDataException)
    {
    }
    return Primitive.NULL;
  }

  private int columnToInt(TableData paramTableData, String paramString)
  {
    if (paramTableData != null)
    {
      int i;
      try
      {
        for (i = 0; i < paramTableData.getColumnCount(); ++i)
          if (ComparatorUtils.tableDataColumnNameEquals(paramTableData.getColumnName(i), paramString))
            return (i + 1);
      }
      catch (TableDataException localTableDataException)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
      }
    }
    return -1;
  }

  public Function.Type getType()
  {
    return Function.REPORT;
  }

  public String getCN()
  {
    return "VALUE(tableData,col,row)返回tableData中列号为col的值, 行号为row。\ntableData:tableData的名字，字符串形式的。\nrow:行号。\ncol:列号。\n备注:\n    先从报表数据集中查找，然后再从服务器数据集中查找，返回的是tableData的行数。\n示例:\nVALUE(\"country\"，5，3)等于South America。\nVALUE(\"Simple\"，8，3)等于jonnason。\n示例：VALUE(\"country\"，5，\"大陆\")等于South America。\n\nVALUE(tableData,col)返回tableData中列号为col的一列值。\nVALUE(tableData,targetCol, orgCol, element)返回tableData中第targetCol列中的元素，这些元素对应的第orgCol列的值为element。\n示例：\ntableData : co\n国家 省份\n\n中国 江苏\n中国 浙江\n中国 北京\n美国 纽约\n美国 新泽西\nVALUE(\"co\",2, 1, \"中国\")等于[\"江苏\", \"浙江\", \"北京\"]。\n特别说明：列号也可以写为列名。\nVALUE(tableData,targetCol, orgCol, element, idx)返回VALUE(tableData,targetCol, orgCol, element)数组的第idx个值。\n特别说明：idx的值小于0时，会取数组的第一个值，idx的值大于数组长度时，会取数组的最后一个值。\n";
  }

  public String getEN()
  {
    return "";
  }
}